/*
 * Copyright 2002-2015 the Beijing Brain Technology co.,LTD http://www.bris.cn/
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bris.niosocket.message;

import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * 可以存储信息的消息,具体定义编解码规范,消息数据结构</br>
 * 2015年11月26日</br>
 * @author xunaidong
 * @since 1.0.0
 */
public abstract class StoreMessage implements IMessage,Serializable{
	
	private static final long serialVersionUID = 1L;

	protected  SerialNumber number;//流水号

	protected final Map<String,String[]> store = new HashMap<String, String[]>();
	/**
	 * 
	 * 插入一个键值对<br/> 
	 * 
	 * <code>e.g:{key:OP,values:String[]{"v1","v2"}}</code></br>
	 * 
	 * @author xunaidong 
	 * @param key
	 * @param values 
	 * @since 1.0.0
	 */
	public void push(String key,String... values){
		store.put(key, values);
	}
	/**
	 * 插入一个键值对,且只有唯一值<br/> 
	 * @author xunaidong 
	 * @param key
	 * @param value 
	 * @since 1.0.0
	 */
	public void pushUnique(String key,String value){
		push(key,new String[]{value});
	}
	/**
	 * 
	 * 获取指定键值的值数组 <br/> 
	 * 
	 * @author xunaidong 
	 * @param key
	 * @return 
	 * @since 1.0.0
	 */
	public String[] getValue(String key){
		return store.get(key);
	}
	/**
	 * 
	 * 获取指定键值的值数组 <br/> 
	 * 
	 * @author xunaidong 
	 * @param key
	 * @return 
	 * @since 1.0.0
	 */
	public String getUniqueValue(String key){
		String[] values = store.get(key);
		if(values!=null&&values.length==1)
			return values[0];
		else
			return null;
	}
	
	public Map<String,String[]> getValuesMap(){
		return Collections.unmodifiableMap(store);
	}
	public void setSerialNumber(SerialNumber number){
		this.number = number;
	}

}
